/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatFrame.java
 *
 * Created on 2011-10-11, 16:54:33
 */
package com.lanchat.view;

import com.lanchat.model.data.ChatMessage;
import com.lanchat.model.data.LogInfo;
import com.lanchat.model.data.User;
import com.lanchat.model.util.LocalSaver;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class ChatFrame extends javax.swing.JFrame {
    private User localuser;  //本地用户
    private String userName;
    private String IP;
    /** Creates new form ChatFrame */
    public ChatFrame() {
        initComponents();
        this.setVisible(true);
    }
    
    public ChatFrame(User user,String userIP){
        this.localuser=user;
        this.userName=userIP.substring(0,userIP.indexOf(':'));
        this.IP=userIP.substring(userIP.indexOf(':')+1);
        MainListFrame.chatList.put(IP, this);
        initComponents();
        IPLabel.setText(IPLabel.getText()+IP);
        UserNameLabel.setText(UserNameLabel.getText()+userName);
        this.setVisible(true);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        IPLabel = new javax.swing.JLabel();
        UserNameLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        MessageArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        ChatArea = new javax.swing.JTextArea();
        SendButton = new javax.swing.JButton();
        CloseButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        IPLabel.setText("IP:");

        UserNameLabel.setText("用户名:");

        jLabel4.setText("欢迎使用本软件，有问题请致电张哲");

        MessageArea.setColumns(20);
        MessageArea.setRows(5);
        jScrollPane1.setViewportView(MessageArea);

        ChatArea.setColumns(20);
        ChatArea.setEditable(false);
        ChatArea.setRows(5);
        jScrollPane2.setViewportView(ChatArea);

        SendButton.setText("发送");
        SendButton.setName("SendButton"); // NOI18N
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });

        CloseButton.setText("关闭");
        CloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(IPLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(UserNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(SendButton)
                        .addGap(53, 53, 53)
                        .addComponent(CloseButton)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(IPLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(UserNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SendButton)
                    .addComponent(CloseButton))
                .addGap(15, 15, 15))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
        // TODO add your handling code here:
        String msg=MessageArea.getText();
        if(msg == null ? "" == null : msg.equals("")){
            JOptionPane.showMessageDialog(null, "请输入消息！");
            return;
        }
        ChatMessage message=new ChatMessage(msg, localuser.getUserName(), localuser.getAddress(), ChatMessage.MSG);
        MainListFrame.send.send(message, IP);
        ChatArea.append(localuser.getUserName()+":\n"+msg);
        MessageArea.setText("");
    }//GEN-LAST:event_SendButtonActionPerformed

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseButtonActionPerformed
        // TODO add your handling code here:
        MainListFrame.chatList.remove(IP);
        this.dispose();
    }//GEN-LAST:event_CloseButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        MainListFrame.chatList.remove(IP);
    }//GEN-LAST:event_formWindowClosing

    public void AddMessage(ChatMessage msg){
        String temp="\n" + msg.getUser()+":\n"+msg.getMsg();
        ChatArea.append(temp);
        User u=new User();
        u.setAddress(msg.getIP());
        u.setUserName(msg.getUser());
        LogInfo log=new LogInfo(msg.getMsg(), new Date(),u);
        LocalSaver saver=new LocalSaver();
        saver.SaveMessage(log);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea ChatArea;
    private javax.swing.JButton CloseButton;
    private javax.swing.JLabel IPLabel;
    private javax.swing.JTextArea MessageArea;
    private javax.swing.JButton SendButton;
    private javax.swing.JLabel UserNameLabel;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
